#include "TollgateEditorOperateLayer.h"
#include "global/GlobalDefine.h"
#include "global/GlobalParam.h"
#include "global/GlobalPath.h"

TollgateEditorOperateLayer::TollgateEditorOperateLayer(){
	m_tollgateEditorLayer = NULL;
	m_lvlLabel = NULL;
}

TollgateEditorOperateLayer::~TollgateEditorOperateLayer(){
	CC_SAFE_RELEASE(m_tollgateEditorLayer);
	CC_SAFE_RELEASE(m_lvlLabel);
}

TollgateEditorOperateLayer* TollgateEditorOperateLayer::create(TollgateEditorLayer* tollgateEditorLayer){
	TollgateEditorOperateLayer* tollgateEditorOperateLayer = new TollgateEditorOperateLayer();
	if(tollgateEditorOperateLayer && tollgateEditorOperateLayer->init(tollgateEditorLayer)){
		tollgateEditorOperateLayer->autorelease();
	} else{
		CC_SAFE_DELETE(tollgateEditorOperateLayer);
		tollgateEditorOperateLayer = NULL;
	}
	return tollgateEditorOperateLayer;
}

bool TollgateEditorOperateLayer::init(TollgateEditorLayer* tollgateEditorLayer){
	bool bRet = false;
	do{
		CC_BREAK_IF(!CCLayer::init());

		m_tollgateEditorLayer = tollgateEditorLayer;
		CC_SAFE_RETAIN(m_tollgateEditorLayer);

		m_lvlLabel = CCLabelTTF::create("",PATH_FONT_PUBLIC,GlobalParam::PublicFontSizeNormal);
		CC_SAFE_RETAIN(m_lvlLabel);

		addWins();
		bRet = true;
	}while(0);

	return bRet;
}

void TollgateEditorOperateLayer::addWins(){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	CCControlButton* saveBtn = this->createControlButton(
		PATH_BTN_save,PATH_FONT_PUBLIC,GlobalParam::PublicFontSizeNormal,
		PATH_PNG_PUBLIC_btn_down,PATH_PNG_PUBLIC_btn_up);
	saveBtn->setPosition(ccp(visibleSize.width - saveBtn->getContentSize().width, visibleSize.height - saveBtn->getContentSize().height));
	saveBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(TollgateEditorOperateLayer::saveToPlistFile),CCControlEventTouchUpInside);
	this->addChild(saveBtn,20);

	CCControlButton* nextBtn = this->createControlButton(
		PATH_BTN_nextLvl,PATH_FONT_PUBLIC,GlobalParam::PublicFontSizeNormal,
		PATH_PNG_PUBLIC_btn_down,PATH_PNG_PUBLIC_btn_up);
	nextBtn->setPosition(ccp(saveBtn->getPositionX() - saveBtn->getContentSize().width/2 - nextBtn->getContentSize().width, saveBtn->getPositionY()));
	nextBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(TollgateEditorOperateLayer::nextLvl),CCControlEventTouchUpInside);
	this->addChild(nextBtn,20);

	CCControlButton* preBtn = this->createControlButton(
		PATH_BTN_preLvl,PATH_FONT_PUBLIC,GlobalParam::PublicFontSizeNormal,
		PATH_PNG_PUBLIC_btn_down,PATH_PNG_PUBLIC_btn_up);
	preBtn->setPosition(ccp(nextBtn->getPositionX() - nextBtn->getContentSize().width/2 - preBtn->getContentSize().width, nextBtn->getPositionY()));
	preBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(TollgateEditorOperateLayer::preLvl),CCControlEventTouchUpInside);
	this->addChild(preBtn,20);

	CCControlButton* changeBtn = this->createControlButton(
		PATH_BTN_change,PATH_FONT_PUBLIC,GlobalParam::PublicFontSizeNormal,
		PATH_PNG_PUBLIC_btn_down,PATH_PNG_PUBLIC_btn_up);
	changeBtn->setPosition(ccp(preBtn->getPositionX() - preBtn->getContentSize().width/2 - changeBtn->getContentSize().width, preBtn->getPositionY()));
	changeBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(TollgateEditorOperateLayer::changePosType),CCControlEventTouchUpInside);
	this->addChild(changeBtn,20);

	CCString* lvlStr = CCString::createWithFormat(PATH_BTN_lvlLabel,m_tollgateEditorLayer->getCurLevel());
	m_lvlLabel->setString(lvlStr->getCString());
	m_lvlLabel->setPosition(ccp(saveBtn->getPositionX() , saveBtn->getPositionY() - saveBtn->getContentSize().height/2 - m_lvlLabel->getContentSize().height));
	this->addChild(m_lvlLabel,20);

}

CCControlButton* TollgateEditorOperateLayer::createControlButton(
	const char* text,const char* font,int fontSize,
	const char* normalBtn,const char* highLightBtn){

	CCLabelTTF* btnTTF = CCLabelTTF::create(text,font,fontSize);
	CCScale9Sprite* normalSprite = CCScale9Sprite::create(normalBtn);
	CCScale9Sprite* highLightSprite = CCScale9Sprite::create(highLightBtn);

	CCControlButton* controlBtn = CCControlButton::create(btnTTF,normalSprite);
	controlBtn->setBackgroundSpriteForState(highLightSprite,CCControlStateHighlighted);

	return controlBtn;
}

void TollgateEditorOperateLayer::saveToPlistFile(CCObject* pSender, CCControlEvent event){
	m_tollgateEditorLayer->saveToPlistFile();
}

void TollgateEditorOperateLayer::preLvl(CCObject* pSender, CCControlEvent event){
	m_tollgateEditorLayer->preLvl();
	CCString* lvlStr = CCString::createWithFormat(PATH_BTN_lvlLabel,m_tollgateEditorLayer->getCurLevel());
	m_lvlLabel->setString(lvlStr->getCString());
}

void TollgateEditorOperateLayer::nextLvl(CCObject* pSender, CCControlEvent event){
	m_tollgateEditorLayer->nextLvl();
	CCString* lvlStr = CCString::createWithFormat(PATH_BTN_lvlLabel,m_tollgateEditorLayer->getCurLevel());
	m_lvlLabel->setString(lvlStr->getCString());
}

void TollgateEditorOperateLayer::changePosType(CCObject* pSender, CCControlEvent event){
	m_tollgateEditorLayer->changePosType();
}
